package br.com.californiahotel.server.serviceImpl;

import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import br.com.californiahotel.client.service.RelatorioService;
import br.com.californiahotel.server.hibernate.DatabaseUtils;
import br.com.californiahotel.server.hibernate.models.Apartamento;
import br.com.californiahotel.shared.models.ServicoFaturadoModel;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class RelatorioServiceImpl extends RemoteServiceServlet implements RelatorioService {

	private static final DatabaseUtils<Apartamento> APTO_DATABASE = new DatabaseUtils<Apartamento>();

	@Override
	public Double taxaByData(Date data) {
		SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		Double aptosOcupados = null;
		try {
			Date parse = df.parse(data.getDate()+"/"+(data.getMonth()+1)+"/"+(data.getYear()+1900));
			aptosOcupados = ((BigInteger) APTO_DATABASE.executeQueryUniqueResult("select count(*) from reserva l where l.data=?",data)).doubleValue();
			Date date = df.parse(new Date().getDate()+"/"+(new Date().getMonth()+1)+"/"+(new Date().getYear()+1900));
			if(parse.before(date)){
				aptosOcupados = ((BigInteger) APTO_DATABASE.executeQueryUniqueResult("select count(*) from locacao l where l.data=?",data)).doubleValue();
			}else{
				if(!parse.after(date)){
					aptosOcupados += ((BigInteger) APTO_DATABASE.executeQueryUniqueResult("select count(*) from locacao l where l.data=?",data)).doubleValue();
				}
			}
		} catch (ParseException e) {
			e.printStackTrace();
			return 0d;
		}
		List<Apartamento> findAll = APTO_DATABASE.findAll(Apartamento.class);
		Integer aptosTotal = findAll.size();
		return (aptosOcupados/(double)aptosTotal);
	}

	@Override
	public List<Double> evolucaoValorFaturado() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer aptoByHospede(String hospede) {
		Integer aptoByHospede = (Integer) APTO_DATABASE.executeQueryUniqueResult("select l.apartamento_numeroApartamento from reserva l where l.hospede=?",hospede);
		return aptoByHospede;
	}

	@Override
	public List<ServicoFaturadoModel> servicosUltimoMes() {
		// TODO Auto-generated method stub
		return null;
	}
	
}
